package com.version.manager.service.Shop;

import com.version.manager.entity.Shop.shopCategoryDetail;
import com.version.manager.entity.Shop.viewShopGoods;
import com.version.manager.mapper.Shop.shopCategoryDetailMapper;
import com.version.manager.model.Msg;
import com.version.manager.service.BaseService;
import com.version.manager.util.QueryParam;
import com.version.manager.vo.categoryDetailChild;
import com.version.manager.vo.categoryDetailInfo;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class shopCategoryDetailService extends BaseService<shopCategoryDetailMapper, shopCategoryDetail> {

    @Autowired
    private viewGoodsService service;
    private static org.slf4j.Logger logger= LoggerFactory.getLogger(shopCategoryService.class);
    public Msg save(shopCategoryDetail data){
        try{
            return super.saveOrUpdate(data);

        }catch (Exception ex){
            logger.error(ex.getMessage());
            return Msg.fail().addMessage(ex.getMessage());
        }
    }


    @Cacheable(value = "categoryDetail",key = "'categroyDetail_'.concat(#root.args[0])",sync=true)
    public Msg getDetail(String categoryId){
        shopCategoryDetail e=new shopCategoryDetail();
        e.setCategoryId(categoryId);
        Map<String,Object> mapPara=new HashMap<>();
        mapPara.put("categoryId",categoryId);
        QueryParam param=new QueryParam(mapPara);
        List<shopCategoryDetail> list= super.selectList(param);
        Map<String,List<shopCategoryDetail>> map=new HashMap<String,List<shopCategoryDetail>>();
        for(shopCategoryDetail it :list){

            if(!map.containsKey(it.getSex())){
                map.put(it.getSex(),new ArrayList<>());
            }
            map.get(it.getSex()).add(it);
        }
        List<categoryDetailInfo> result=new ArrayList<>();
        for(String k:map.keySet()){
            categoryDetailInfo info=new categoryDetailInfo();
            info.setName(k);
            List<categoryDetailChild> children=new ArrayList<>();
            for(shopCategoryDetail it:map.get(k)){
                categoryDetailChild io=new categoryDetailChild(it.getDetailName(),it.getDetailImage(),it.getId());
                children.add(io);

            }
            info.setChildren(children);
            result.add(info);
        }
        return Msg.success().addData(result);
    }


}
